﻿Imports Tool
Imports RunEnv
Imports System.Windows.Forms

Public Class dlgLogin
    Private mstrSystemType As String
    Private mstrAccountDataBase As String
    Private mstrAccountServer As String
    Private mdtAccount As DataTable
    Private mstrCurAccountName As String
    Private mobjUser As CUser

    Public Function ShowOpen(ByVal systemType As String) As CUser
        mstrSystemType = systemType
        Me.Text = systemType & "登陆"
        If Not InitAccountList() Then
            Return Nothing
        End If
        textUserID.Text = Tool.CToolFileIni.ReadKey("Main", "UserID", CRunEnv.AppPath & "\" & "system.ini")

        Me.ShowDialog()
        Return mobjUser
    End Function

    Private Function InitAccountList() As Boolean
        Dim sql As String
        Dim sqlCond As String
        Dim strMode As String
        Dim mode As Integer
        Dim accountName As String
        strMode = Tool.CToolFileIni.ReadKey("Main", "LoginMode", CRunEnv.AppPath & "\" & "system.ini")
        mode = CInt(strMode)

        Select Case mode
            Case 1
                sqlCond = "IsCurrent <> 0"
            Case 2
                sqlCond = "IsCurrent = 0"
            Case Else
                sqlCond = ""
        End Select
        If Len(mstrSystemType) > 0 Then
            sqlCond = CToolADO.AddSqlCond(sqlCond, "SystemType ='" & mstrSystemType & "'")
        End If
        sql = "Select * From A_AccountInfo" _
            & " Where IsRun <> 0"
        If Len(sqlCond) > 0 Then
            sql = CToolADO.AddSQL_Where(sql, sqlCond)
        End If
        If CRunEnv.DBConnection Is Nothing Then
            Return False
        End If
        mdtAccount = CRunEnv.DBConnection.GetDataTable(sql)

        ComboBox1.Items.Clear()
        For Each dataRow As DataRow In mdtAccount.Rows
            ComboBox1.Items.Add(dataRow.Item("AccountName"))
        Next
        If ComboBox1.Items.Count > 0 Then
            ComboBox1.SelectedIndex = 0
        End If
        accountName = Trim(Tool.CToolFileIni.ReadKey("Main", "AccountName", CRunEnv.AppPath & "\" & "system.ini"))
        If Len(accountName) > 0 Then
            For Each item As String In ComboBox1.Items
                If String.Compare(accountName, item) = 0 Then
                    ComboBox1.SelectedItem = item
                    Exit For
                End If
            Next
        End If
        Return True
    End Function

    Private Sub CmdOK_Click(sender As Object, e As System.EventArgs) Handles CmdOK.Click
        Dim userID As String
        Dim pwd As String
        Dim accountName As String
        Try
            userID = Trim(textUserID.Text)
            pwd = Trim(textPwd.Text)

            If Len(userID) <= 0 Then
                MsgBox("请输入用户名!", vbInformation, "系统提示")
                Exit Sub
            End If

            accountName = ComboBox1.Text
            If Len(accountName) <= 0 Then
                MsgBox("请选择一个帐套 !", MsgBoxStyle.Exclamation, "系统提示")
                Exit Sub
            End If

            If Not GetCurrentAccountCnn() Then
                Exit Sub
            End If

            If CheckUser(userID, pwd) Then
                CRunEnv.SystemType = mstrSystemType
                CRunEnv.CurrentUser = mobjUser
                CRunEnv.AccountName = ComboBox1.Text
                CRunEnv.AccountServer = mstrAccountServer
                CRunEnv.AccountDataBase = mstrAccountDataBase

                Tool.CToolFileIni.WriteKey("Main", "UserID", Trim(textUserID.Text), CRunEnv.AppPath & "\" & "system.ini")
                Tool.CToolFileIni.WriteKey("Main", "AccountName", Trim(ComboBox1.Text), CRunEnv.AppPath & "\" & "system.ini")
                Me.Close()
            End If
        Catch ex As Exception
            MsgBox("用户验证失败，" & ex.Message, MsgBoxStyle.Exclamation, "系统提示")
            mobjUser = Nothing
        End Try
        
    End Sub

    Private Function GetCurrentAccountCnn() As Boolean
        Dim accountName As String
        Dim uid As String
        Dim pwd As String
        Dim dataRow As DataRow
        accountName = ComboBox1.Text
        If accountName <> mstrCurAccountName Then
            dataRow = CToolADO.FindInDataTable(mdtAccount, "AccountName", accountName)
            mstrAccountServer = dataRow.Item("AccountServer")
            mstrAccountDataBase = dataRow.Item("AccountdataBase")
            uid = CDBKeyMana.DataCompress(dataRow.Item("AccountUserID"), False)
            pwd = CDBKeyMana.DataCompress(dataRow.Item("AccountPwd"), False)
            CRunEnv.DBConnection = New CDBConnection(mstrAccountServer, mstrAccountDataBase, uid, pwd)
            If CRunEnv.DBConnection.sqlConnection Is Nothing Then
                Return False
            End If
            mstrCurAccountName = accountName
        End If
        Return True
    End Function

    Private Function CheckUser(ByVal userID As String, pwd As String) As Boolean
        Dim sql As String
        Dim userDataTable As DataTable
        Dim userPwd As String
        Try
            sql = "Select * From B_User Where UserID ='" & userID & "'"
            userDataTable = CRunEnv.DBConnection.GetDataTable(sql)
            If userDataTable.Rows.Count <> 1 Then
                MsgBox("不存在该用户!", MsgBoxStyle.Exclamation, "系统提示")
                Return False
            End If

            If CBool(userDataTable.Rows(0).Item("isStop")) Then
                MsgBox("该用户已停用", MsgBoxStyle.Exclamation, "系统提示")
                Return False
            End If

            If IsDBNull(userDataTable.Rows(0).Item("PassWord")) Then
                userPwd = ""
            Else
                userPwd = CDBKeyMana.DataCompress(userDataTable.Rows(0).Item("PassWord"), False)
            End If

            If userPwd <> pwd Then
                MsgBox("密码不正确！", MsgBoxStyle.Exclamation, "系统提示")
                Return False
            End If

            mobjUser = New CUser
            mobjUser.UserName = IIf(IsDBNull(userDataTable.Rows(0).Item("UserName")), "", userDataTable.Rows(0).Item("UserName"))
            mobjUser.UserID = userID
            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
    End Function

    Private Sub textUserID_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles textUserID.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            textPwd.Focus()
        End If
    End Sub

    Private Sub textPwd_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles textPwd.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            CmdOK.Focus()
        End If
    End Sub

    Private Sub CmdCancel_Click(sender As System.Object, e As System.EventArgs) Handles CmdCancel.Click
        Me.Close()
    End Sub
End Class